/*
 *
 *
 *
 */
#include "SmileManager.h"

#ifndef _SMILEMANAGER_CPP_
#define _SMILEMANAGER_CPP_

using namespace std;
 
/*
 *
 */
SmileManager::SmileManager(string fileName):numSmileSet(0)
{
    file.open(fileName.c_str());
    if (!file) {
        cerr << "Unable to open file " << fileName << endl;
    }
    else
    {
        char buffer[BUFFER_SIZE] = {'\0'};
        numSmiles = 0;
        while (!file.eof()) 
        {
                file.getline(buffer,BUFFER_SIZE);                                     /*le uma linha do arquivo*/
                if((buffer[0] != '\0') && (buffer[0] != '\n'))                        /*verifica se a linha não esta vazia*/
                {
                    SmileSet *smileSetAux = (SmileSet*) malloc(sizeof(SmileSet));     /*aloca espaço para um smileSet*/
                    string str(buffer);
                    istringstream iss(buffer);
                    iss >> smileSetAux->initialTime;
                    iss >> smileSetAux->finalTime;
                    iss >> smileSetAux->deltaTime;
                    iss >> smileSetAux->number;
                    numSmiles += smileSetAux->number;
                    iss >> smileSetAux->size;
                    iss >> smileSetAux->file;
                    iss >> smileSetAux->ax;
                    iss >> smileSetAux->ay;
                    iss >> smileSetAux->vx;
                    iss >> smileSetAux->vy;
                    iss >> smileSetAux->x;
                    iss >> smileSetAux->y;
                    iss >> smileSetAux->distance;
cout << smileSetAux->initialTime  << " " << smileSetAux->number << " " <<
smileSetAux->size << " " << smileSetAux->file << " " <<smileSetAux->ax << " " <<
smileSetAux->ay <<  " "<< smileSetAux->vx << " " << smileSetAux->vy << " " <<
smileSetAux->x << " " << smileSetAux->y << " " << smileSetAux->distance << endl;
                    smileSets[numSmileSet++] = smileSetAux;                   /*incrementa o número de smileSet e insere os dados da linha no array de smileSets*/
                }
                buffer[0] = '\0';                   /*zera o buffer*/
        }
        file.close();
        
        /*A seguir é criado um array de smile, contendo todos os smiles do arquivo stage*.txt*/
        int i, j, indexSmile=0;
        int _x,_y;
        arraySmiles.clear();
        arraySmiles.resize(numSmiles);
        for(i = 0; i< numSmileSet; i++ )
        {
//cout << i << endl;
            float veloxModule = sqrt(pow(smileSets[i]->vx,2) + pow(smileSets[i]->vy,2));
            for(j = 0; j < smileSets[i]->number ; j++)
            {
                /*utiliza semelhança de triangulo com o vetor velocidade para calcular as posições x e y*/
                //calculo para a posição x
                if(smileSets[i]->vx == 0)
                {
                    _x = smileSets[i]->x;
                }
                else
                {
                    _x = (smileSets[i]->vx/veloxModule)*(j*-1*smileSets[i]->distance)+smileSets[i]->x;
                }
                //calculo para a posição y
                if(smileSets[i]->vy == 0)
                {
                    _y = smileSets[i]->y;
                }
                else
                {
                    _y = (smileSets[i]->vy/veloxModule)*(j*-1*smileSets[i]->distance)+smileSets[i]->y;
                }
                //calculo do tempo inicial
                int initialTime = smileSets[i]->initialTime + (j*smileSets[i]->deltaTime);
                //calculo do tempo final
                int finalTime = smileSets[i]->finalTime + (j*smileSets[i]->deltaTime);
                Smiley smile(Smiley(smileSets[i]->file,smileSets[i]->size, smileSets[i]->ax, smileSets[i]->ay, smileSets[i]->vx, smileSets[i]->vy, _x, _y, initialTime, finalTime));
                arraySmiles[indexSmile++] = smile;
            }
        }
    }
    catchSfx = new Audio("../Sounds/coin.wav",0);
    clock.start();
}

/*
 *
 */
SmileManager::~SmileManager(void)
{
    int i;
    if(catchSfx != 0)
    {
       delete catchSfx;
       catchSfx = 0; 
    }
    for(i = 0; i< numSmileSet; i++)
    {
        SmileSet *smileSetAux = smileSets[i];
        free(smileSetAux);
    }
    arraySmiles.clear();
}

/*
 * 
 */
int SmileManager::update(int tDelta,Sprite *leftHand, Sprite *rightHand)
{
    int numColisions = 0;
    for(vector<Smiley>::iterator it = arraySmiles.begin(); it != arraySmiles.end(); it++)
    {
        /*verifica se esta na hora de mostrar o smile*/
        if((clock.getTime() >= it->getInitialTime()) && it->getAlive())
        {
            /*verifica se o smile esta dentro dos limites da janela*/
            SDL_Rect destRect = it->getRect();
            if((destRect.x + destRect.w/2 >= 0) && (destRect.x - destRect.w/2 <= WINDOW_WIDTH) && (destRect.y + destRect.h/2 >= 0) && (destRect.y - destRect.h/2 <= WINDOW_HEIGTH))
            {
                it->setShow(true);
            }
            if(it->sphereCollidesWith(leftHand))
            {
                catchSfx->play(0);
                it->setAlive(false);
                it->setShow(false);
                ++numColisions;
            }
            else if(it->sphereCollidesWith(rightHand))
            {
                catchSfx->play(0);
                it->setAlive(false);
                it->setShow(false);
                ++numColisions;
            }
            else if(clock.getTime() >= it->getFinalTime())
            {
                it->setAlive(false);
                it->setShow(false);
            }
            else
            {
                it->update(tDelta);
            }
        }
    }
    return numColisions;
}

/*
 * 
 */
void SmileManager::render(SDL_Surface *screen)
{
    for(vector<Smiley>::iterator it = arraySmiles.begin(); it != arraySmiles.end(); it++)
    {
        it->render(screen);
    }
}

#endif
